<?php
require_once("../../resources/commons.php");
require_once("dashboard_commons.php");
require_once("../../data/User.php");
require_once("../../data/Item.php");
require_once("../../data/Comment.php");
require_once("HTML/QuickForm/Controller.php");
require_once("HTML/QuickForm/Action/Display.php");
require_once("HTML/QuickForm/Action/Submit.php");
require_once("HTML/QuickForm/textarea.php");
require_once("HTML/QuickForm/link.php");
require_once("HTML/QuickForm/hidden.php");
require_once("HTML/QuickForm/submit.php");
require_once("HTML/QuickForm/button.php");

/**
 * Comment creation form.<br/>
 * Form to be used by a user to add a comment to an action.
 */
class AddCommentPage extends HTML_QuickForm_Page{
    function buildForm(){
        $this->_formBuilt = true;
		
		$l_commentInput=new HTML_QuickForm_textarea("note");
		$l_commentInput->setLabel("Note :");
		$l_commentInput->setCols(60);
		$l_commentInput->setRows(5);
		$this->addElement($l_commentInput);
		
		$l_itemInput=new HTML_QuickForm_hidden("itemId");
		$this->addElement($l_itemInput);
		
		$l_submit=new HTML_QuickForm_submit("submit");
		$l_submit->setValue("OK");
		$this->addElement($l_submit);
		
		$l_cancel=new HTML_QuickForm_submit($this->getButtonName("cancel"));
		$l_cancel->setValue("Cancel");
		$this->addElement($l_cancel);
		
		//The validation rules (either client and server)
		$this->addRule("note","Comment note must not be empty","required",null,"server");
		
		$this->applyFilter("note","trim");
		
        $this->setDefaultAction("submit");
    }
}

/**
 * We subclass the default 'display' handler to customize the output
 */
class ActionDisplay extends HTML_QuickForm_Action_Display{
    function _renderForm($page){   	
    	SessionManager::getInstance()->checkSession();
    	displayTop("Add new comment","../../resources/htmledit.php",null);
        $page->display();
        displayBottom();
    }
}

/**
 * Action to be called after a submit action.<br>
 * Send an invitation to the user with a link
 * to be used to access the registration screen.
 */
class ActionProcess extends HTML_QuickForm_Action{
    function perform($page){
    	SessionManager::getInstance()->checkSession();
    	
    	//Actions can be commented out only by the creator of the action or the user allocated with the action
    	$l_action=Item::getFromPrimaryKey(base64_decode($page->exportValue("itemId")));
		$l_me=SessionManager::getCurrentLoggedUserId();
		if($l_action->isAccessAllowed($l_me)){
	    	$l_comment=new Comment();
	    	$l_comment->setNote($page->exportValue("note"));
	    	$l_comment->setItem_id(base64_decode($page->exportValue("itemId")));
	    	$l_comment->setCreatingUser_id(SessionManager::getCurrentLoggedUserId());
	    	$l_comment->setCreated(date("Y/m/d", time()));
	    	$l_comment->save();
	    	
	    	notifyNewComment($l_action->getAllocatedUser_Id_Ref()->getEmail(),SessionManager::getCurrentLoggedUserDisplayName(),$l_action->getId(),$l_comment);
		}
    	
    	HTTP::redirect("index.php");
    }
}

class CancelProcess extends HTML_QuickForm_Action{
    function perform($page){
    	HTTP::redirect("index.php");
    }
}

$controller =new HTML_QuickForm_Controller('CommentCreationProcess', false);
$controller->container(true);
$controller->addPage(new AddCommentPage('commentCreationPage'));
$controller->setDefaults(array(
		"itemId" => $_REQUEST["id"]));
$controller->addAction('display', new ActionDisplay());
$controller->addAction('process', new ActionProcess());
$controller->addAction('cancel', new CancelProcess());
$controller->addAction('submit', new HTML_QuickForm_Action_Submit());

$controller->run();

?>